package com.aoip.telemetry.telemetry.configureui.business.analysis.element.service.impl;

import com.aoip.telemetry.telemetry.configureui.business.analysis.element.mapper.ElementConfigMapper;
import com.aoip.telemetry.telemetry.configureui.business.analysis.element.service.ElementService;
import com.aoip.telemetry.telemetry.core.redis.RedisCache;
import com.aoip.telemetry.telemetry.entity.element.ElementConfigEntity;
import com.aoip.telemetry.telemetry.entity.enums.CacheKeyEnum;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * @author chenbai
 * @version 1.0
 * @date 2021/12/28 14:14
 */
@Service
@DS("telemetry")
public class ElementServiceImpl implements ElementService {

    @Resource
    private ElementConfigMapper mapper;
    @Resource
    private RedisCache redisCache;

    @Override
    public List<ElementConfigEntity> list(ElementConfigEntity entity) {
        QueryWrapper<ElementConfigEntity> queryWrapper = new QueryWrapper<>();
        String identifierMsg = entity.getIdentifierMsg();
        String tableName = entity.getTableName();
        String fieldName = entity.getFieldName();
        if (StringUtils.hasText(identifierMsg)) {
            queryWrapper.like("identifier_msg", identifierMsg);
        }
        if (StringUtils.hasText(tableName)) {
            queryWrapper.like("table_name", tableName);
        }
        if (StringUtils.hasText(fieldName)) {
            queryWrapper.like("field_name", fieldName);
        }
        return mapper.selectList(queryWrapper);
    }

    @Override
    public ElementConfigEntity element(String id) {
        return mapper.selectById(id);
    }

    @Override
    public int insert(ElementConfigEntity entity) {
        redisCache.setCacheObject(CacheKeyEnum.ELEMENT + ":" + entity.getId(), entity);
        toLowerCase(entity);
        return mapper.insert(entity);
    }

    @Override
    public int update(ElementConfigEntity entity) {
        redisCache.setCacheObject(CacheKeyEnum.ELEMENT + ":" + entity.getId(), entity);
        toLowerCase(entity);
        return mapper.updateById(entity);
    }


    @Override
    public int initCache() {
        List<ElementConfigEntity> list = list(new ElementConfigEntity());
        Collection<String> keys = redisCache.keys(CacheKeyEnum.ELEMENT + ":*");
        redisCache.deleteObject(keys);
        list.forEach(item -> {
            toLowerCase(item);
            redisCache.setCacheObject(CacheKeyEnum.ELEMENT + ":" + item.getId(), item);
        });
        return list.size();
    }

    @Override
    public void delete(Long[] ids) {
        for (long id : ids) {
            redisCache.deleteObject(CacheKeyEnum.ELEMENT + ":" + id);
            mapper.deleteById(id);
        }
    }

    /**
     * 将表名字段名称转为小写
     *
     * @param entity 元素实体
     * @author chenbai
     * @date 2022/12/11
     **/
    private void toLowerCase(ElementConfigEntity entity) {
        String tableName = entity.getTableName();
        if (StringUtils.hasText(tableName)) {
            entity.setTableName(tableName.toLowerCase());
        }
        String fieldName = entity.getFieldName();
        if (StringUtils.hasText(fieldName)) {
            entity.setFieldName(fieldName.toLowerCase());
        }
    }
}
